/*
 * Copyright (c) 2020, yifengling0. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. No personal names or organizations' names associated with the
 *    Atomthreads project may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE ATOMTHREADS PROJECT AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "atom.h"
#include "board.h"
#include "stm8l15x_clk.h"
#include "stm8l15x_spi.h"
#include "drv_common.h"
#include "event.h"
#include "drv_usart.h"
#include "drv_pin.h"
#include "drv_spi.h"
#include "simspi.h"
#include "drv_rtc.h"

#ifdef BSP_USE_SIMSPI 
static SimSpi _simSpi;
static SimSpiDevCtrl _simSpiCtrl = {
    38,
    28,
    29,
    39,
    0,
    1
};
#endif

#ifdef COM_USE_DBGPORT
static char _debugPortRxBuffer[BSP_DBGPORT_RX_BUFSIZE] = {0};

static Stm8lUsartConfig _usart3_config = {
    USART3_INDEX,
    CLK_Peripheral_USART3,
    49, //GPIOG 0
    48, //GPIOG 1
    {
        115200,
        8,
        ssb1Bit,
        spNone,
        enSioBlockingSend
    },
    _debugPortRxBuffer,
    NULL
};

static Stm8lUsart _usart;

#endif
/*
    int cs;
    int mosi;
    int miso;
    int scl;
    int autoCs;
*/

#ifdef PKG_USE_A7139    
static Stm8Spi _SpiA7139;
static Stm8lSpiDevCtrl _spiCtrlA7139 = {
    CLK_Peripheral_SPI1,
    12,
    14,
    15,
    13,
    0
};
#endif

#ifdef BSP_USE_RTC  
static Stm8lRtc _rtc;
#endif

void init_devices(void)
{
    DrvCommonInit();

#ifdef BSP_USE_PIN
    Stm8lPinCreate();
#endif    

#ifdef COM_USE_DBGPORT    
    Stm8lUartCreate(&_usart, BSP_DBGPORT_DEVICE_NAME, &_usart3_config, BSP_DBGPORT_RX_BUFSIZE, 0);
#endif
    
#ifdef BSP_USE_SIMSPI    
    SimSpiCreate(&_simSpi, "simSpi", 0, 8, 8, 100000, &_simSpiCtrl, NULL);
#endif
    
#ifdef PKG_USE_A7139    
    Stm8lCreate(&_SpiA7139, PKG_A7139_DEVICE, SPI1, SPI_Mode_Master, SPI_CPOL_Low, SPI_CPHA_1Edge, SPI_Direction_1Line_Tx, 8, 125000, &_spiCtrlA7139);
#endif   

#ifdef BSP_USE_RTC  
    Stm8lRtcCreate(&_rtc);
#endif    
}

/**
 * \b clk_init
 *
 * 初始化时钟
 *
 */
static void clk_init()
{
    CLK_DeInit();
    CLK_HSICmd(ENABLE);//启用内部高速振荡器(HSI 16MHz)
    CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSI);
    CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1);
}

/**
 * \b device_init
 *
 * 所有设备初始化
 *
 */
void board_init()
{
    clk_init();
    init_devices();
}